package cn.hsw.platform.controller;

import cn.hsw.platform.core.doc.annotation.AuthPermission;
import cn.hsw.platform.core.base.BaseServiceController;
import cn.hsw.platform.core.dto.PageDto;
import cn.hsw.platform.core.dto.ResultDto;
import cn.hsw.platform.core.redis.RedisUtil;
import cn.hsw.platform.core.util.*;
import cn.hsw.platform.model.UserModel;
import cn.hsw.platform.repository.UserRepository;
import cn.hsw.platform.service.UserService;
import cn.hsw.platform.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器
 * @author 李子木
 * @date 2020-10-30
 */
@RestController
@RequestMapping(value = "/user")
public class UserController extends BaseServiceController<UserService> {

    @Autowired
    UserRepository repository;

    @Autowired
    RedisUtil redisUtil;

    /**
     * 后台用户新增
     * @param userVO 用户信息
     * @return -
     */
    @PostMapping("/add")
    @AuthPermission("manage:user:add")
    public ResultDto<Object> add(@Validated UserVO userVO){

        service.add(userVO);

        return ResultUtil.success();
    }

    /**
     * 删除用户
     * @param userModel 要删除的用户
     */
    @PostMapping("/del/{pid}")
    @AuthPermission("manage:user:del")
    public ResultDto<Object> del(@PathVariable("pid")UserModel userModel){

        Validator.validateNotNull(userModel,"不存在或已删除");

        service.delete(userModel.getPid());

        return ResultUtil.success();
    }

    /**
     * 修改用户信息
     * @param realName 昵称
     * @param remark 备注
     * @param userModel 要修改的对象
     */
    @PostMapping("/edit/{pid}")
    @AuthPermission("manage:user:edit")
    public ResultDto<Object> edit(@PathVariable("pid")UserModel userModel,
                                  @RequestParam(value = "real_name",required = false)String realName, @RequestParam(required = false) String remark,
                                  @RequestParam(value = "role_pid",required = false)Long [] rolePid){

        Validator.validateNotNull(userModel,"不存在或已删除");

        service.edit(userModel,realName,remark,rolePid);

        return ResultUtil.success();
    }

    /**
     * 获取分页列表
     * page 页码 选填 不传使用默认值
     * size 数量 选填 不传使用默认值
     * order_by 排序字段 选填 不传使用默认值
     * direction 排序类型 选填 不传使用默认值
     * @return 分页列表
     */
    @PostMapping("/list")
    @AuthPermission("manage:user:list")
    public ResultDto<PageDto<UserModel>> list(@RequestParam(defaultValue = "",name = "user_name")String userName,@RequestParam(defaultValue = "")String phone){
        Page<UserModel> page = service.findByUserNameAndPhoneLike(userName,phone,PageUtil.pageable());

        return ResultUtil.success(page.getContent(),page.getTotalElements());
    }

    /**
     * 重置密码
     * @param userModel 要修改的对象
     */
    @PostMapping("/reset/password/{pid}")
    @AuthPermission("manage:user:reset:password")
    public ResultDto<Object> edit(@PathVariable("pid")UserModel userModel){


        Validator.validateNotNull(userModel,"不存在或已删除");

        String password = service.resetPassword(userModel);

        return ResultUtil.success(password);
    }

    @PostMapping("/info/{pid}")
    @AuthPermission("manage:user:info")
    public ResultDto<Object> info(@PathVariable("pid")Long pid){

        Validator.validateNotNull(pid,"主键ID必填");

        UserModel userModel = service.findById(pid);

        Validator.validateNotNull(userModel,"不存在或已删除");

        return ResultUtil.success(userModel);
    }
}
